package com.neusoft.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.dto.EmpDto;
import com.neusoft.exception.EmpSelectException;
import com.neusoft.mapper.*;
import com.neusoft.model.*;
import com.neusoft.service.EmpService;
import com.neusoft.vo.DeptEmpVo;
import com.neusoft.vo.EmpPositionVo;
import com.neusoft.vo.EmpVo;
import com.neusoft.vo.PositionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cpx
 * @date 2025/1/8
 * @Description
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpMapper empMapper;

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private PositionMapper positionMapper;
    @Autowired
    private AuthorityMapper authorityMapper;
    @Autowired
    private DeptEmpMapper deptEmpMapper;
    @Autowired
    private EmpPositionMapper empPositionMapper;

    @Override
    public IPage<EmpVo> selectEmp(Integer pageSize, Integer pageNum, EmpDto empDto) {

        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }

        Page page = new Page(pageNum,pageSize);

        QueryWrapper qw = new QueryWrapper();
        if (empDto.getEmpname() != null && empDto.getEmpname().length() > 0) {
            qw.eq("empname", empDto.getEmpname());
        }
        if (empDto.getDeptId() != null && empDto.getDeptId() > 0) {
            qw.eq("dept_id", empDto.getDeptId());
        }
        if (empDto.getPositionId() != null && empDto.getPositionId() > 0) {
            qw.eq("position_id", empDto.getPositionId());
        }
        if (empDto.getStatus() != null && (empDto.getStatus() == 1 || empDto.getStatus() == 2)) {
            qw.eq("status", empDto.getStatus());
        }

        Date start = empDto.getStartEntryDate();
        Date end = empDto.getEndEntryDate();

        if (start != null && end != null) {
            qw.between("entry_date", start, end);
        }

        // 执行分页查询
        IPage<Emp> empPage = empMapper.selectPage(page, qw);

        // 转换为 EmpVo
        List<EmpVo> empVos = empPage.getRecords().stream()
                .map(this::convertToEmpVo)
                .collect(Collectors.toList());

        IPage<EmpVo> empVoPage = new Page<>();
        empVoPage.setRecords(empVos);
        empVoPage.setTotal(empPage.getTotal());
        empVoPage.setSize(empPage.getSize());
        empVoPage.setCurrent(empPage.getCurrent());

        return empVoPage;
    }

    private EmpVo convertToEmpVo(Emp emp) {
        EmpVo empVo = new EmpVo();
        empVo.setId(emp.getId());
        empVo.setNo(emp.getNo());
        empVo.setEmpname(emp.getEmpname());
        empVo.setSex(emp.getSex());
        empVo.setDeptId(emp.getDeptId());
        empVo.setDeptName(deptMapper.selectById(emp.getDeptId()).getDeptname());
        empVo.setPositionId(emp.getPositionId());
        empVo.setPositionName(positionMapper.selectById(emp.getPositionId()).getName());
        empVo.setStatus(emp.getStatus());
        return empVo;
    }

//    @Override
//    public Integer updateEmp(Emp emp) {
//        Integer i = empMapper.updateEmp(emp);
//        return i;
//    }

    @Override
    public Integer updateStatus(Integer id) {
        Integer i = 0;
        if (id != null) {
            Emp emp = new Emp();
            emp.setId(id);
            emp.setStatus(2);
            i = empMapper.updateStatus(emp);
        }
        return i;
    }

    @Override
    public Integer insertOrUpdateEmp(EmpVo empVo) {
        System.out.println(empVo);
        Emp emp = new Emp();
        if (empVo != null) {
            emp.setId(empVo.getId());
            emp.setNo(empVo.getNo());
            emp.setEmpname(empVo.getEmpname());
            emp.setAccount(empVo.getAccount());
            emp.setPassword(empVo.getPassword());
            emp.setEmail(empVo.getEmail());
            emp.setSex(empVo.getSex());
            emp.setMarryStatus(empVo.getMarryStatus());
            emp.setBirthday(empVo.getBirthday());
            emp.setEntryDate(empVo.getEntryDate());
            emp.setWorkStartDate(empVo.getWorkStartDate());
            emp.setPositionId(empVo.getPositionId());
            emp.setDeptId(empVo.getDeptId());
            emp.setAuthorityId(empVo.getAuthorityId());
            emp.setAvatar(empVo.getAvatar());
            emp.setStatus(1);
            if (emp.getBirthday() != null) {
                emp.setAge(DateUtil.ageOfNow(emp.getBirthday()));
            }
            if (emp.getId() != null) {
                return empMapper.updateById(emp);
            }else {
                return empMapper.insert(emp);
            }
        }
        else {
            throw new EmpSelectException( 7003,"用户数据不能为空");
        }
    }

    @Override
    public EmpVo selectEmpById(Integer id) {
        if (id != null) {
            Emp emp = empMapper.selectById(id);
            EmpVo empVo = new EmpVo();
            empVo.setId(emp.getId());
            empVo.setNo(emp.getNo());
            empVo.setEmpname(emp.getEmpname());
            empVo.setAccount(emp.getAccount());
            empVo.setPassword(emp.getPassword());
            empVo.setEmail(emp.getEmail());
            empVo.setSex(emp.getSex());
            empVo.setMarryStatus(emp.getMarryStatus());
            empVo.setBirthday(emp.getBirthday());
            empVo.setEntryDate(emp.getEntryDate());
            empVo.setWorkStartDate(emp.getWorkStartDate());
            empVo.setAuthorityId(emp.getAuthorityId());
            empVo.setAuthorityName(authorityMapper.selectById(emp.getAuthorityId()).getAuthority());
            empVo.setDeptId(emp.getDeptId());
            empVo.setDeptName(deptMapper.selectById(emp.getDeptId()).getDeptname());
            empVo.setPositionId(emp.getPositionId());
            empVo.setPositionName(positionMapper.selectById(emp.getPositionId()).getName());
            empVo.setAvatar(emp.getAvatar());
            empVo.setStatus(emp.getStatus());
            return empVo;
        }
        else {
            throw new EmpSelectException( 7003,"id不能为空");
        }
    }

    @Override
    public List<DeptEmpVo> getEmpDept(Integer id) {
        if (id != null) {
            List<DeptEmpVo> deptEmpVos = new ArrayList<>();
            for (DeptEmp deptEmp : deptEmpMapper.selectList(new QueryWrapper<DeptEmp>().eq("emp_id", id))){
                DeptEmpVo deptEmpVo = new DeptEmpVo();
                deptEmpVo.setEmpId(id);
                deptEmpVo.setEmpName(empMapper.selectById(id).getEmpname());
                deptEmpVo.setDeptId(deptEmp.getDeptId());
                deptEmpVo.setDeptName(deptMapper.selectById(deptEmp.getDeptId()).getDeptname());
                deptEmpVo.setStartDate(deptEmp.getStartTime());
                deptEmpVo.setEndDate(deptEmp.getEndTime());
                deptEmpVos.add(deptEmpVo);
            }
            return deptEmpVos;
        }
        else {
            throw new EmpSelectException( 7003,"id不能为空");
        }
    }

    @Override
    public List<EmpPositionVo> getEmpPosition(Integer id) {
        if (id != null) {
            List<EmpPositionVo> empPositionVos = new ArrayList<>();
            for (EmpPosition empPosition : empPositionMapper.selectList(new QueryWrapper<EmpPosition>().eq("emp_id", id))){
                EmpPositionVo empPositionVo = new EmpPositionVo();
                empPositionVo.setEmpId(empPosition.getEmpId());
                empPositionVo.setEmpName(empMapper.selectById(id).getEmpname());
                empPositionVo.setPositionId(empPosition.getPositionId());
                empPositionVo.setPositionName(positionMapper.selectById(empPosition.getPositionId()).getName());
                empPositionVo.setStartDate(empPosition.getStartTime());
                empPositionVo.setEndDate(empPosition.getEndTime());
                empPositionVos.add(empPositionVo);
            }
            return empPositionVos;
        }
        else {
            throw new EmpSelectException( 7003,"id不能为空");
        }
    }

    @Override
    public List<Dept> getAllDept() {
        return deptMapper.selectList(new QueryWrapper<Dept>().eq("status", 1));
    }

    @Override
    public List<Position> getAllPosition() {
        return positionMapper.selectList(new QueryWrapper<Position>().eq("status", 1));
    }
}
